package com.swm.resource;

import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.swm.model.MyProject;
import com.swm.model.Project;
import com.swm.model.Testcase;
import com.swm.model.User;
import com.swm.service.ProjectService;

@Path("/project")
@Component
public class ProjectResource {
	//public static Logger logger = Logger.getRootLogger();	
	
	@Autowired
	private ProjectService projectService = new ProjectService();

	public ProjectResource() {
		System.out.println("== UserService Created ==");
	}
	


	/**
	 * Add Project 
	 */
	
	@POST
    @Consumes("application/xml")
    public void addProject( Project project) {    	
    	//logger.info( "[CREATE] Project Name = " + project.getName() );		
    	projectService.AddProject( project );    		    		    	
    }
    
	/**
	 * Request Project List
	 */
	@GET
	@Produces("application/xml")
	public List<Project> getProjects() {
		//logger.info( "[REQ] ProjectList " );
		List<Project> projects = projectService.getAllProject();
		return projects;
	}
	
	/**
	 * Add Project User
	 */
	@GET
	@Path("/{projectID}/user/{userID}")
    @Produces("text/plain")
    public String addProjectUser( @PathParam("projectID") String projectID, 
    		@PathParam("userID") String userID ) {    	
		
		//logger.info( "[REQ][CREATE] ProjectUser : ProjectID = " + projectID + ", userID = " + userID );
    	if ( projectService.addProjectUser(projectID, userID) )
    		return "[Add Project User] success";
    	
    	return "[Add Project User] fail";
    }
	
	/**
	 * Request My Project List
	 */
	/*
	@GET 
	@Path("/list/{ID}")
    @Produces("application/xml")
    public List<MyProject> getMyProjectList( @PathParam("ID") String userID ) {
		System.out.println( "[REQ][SELECT] MyProjectList : userID = " + userID );
		return projectService.getProjectList( userID );				
    }
    */
	@GET 
	@Path("/list/{ID}")
    @Produces("text/plain")
    public String getMyProjectList( @PathParam("ID") String userID ) {
		System.out.println( "[REQ][SELECT] MyProjectList : userID = " + userID );
		return projectService.getProjectList( userID );				
    }
	
	/**
	 * Add Testcase in Project
	 * @param testcase
	 * @param userID
	 * @return
	 */
	@POST
	@Path("/testcase/{userID}")
	@Produces("text/plain")
	public String addTestcase( Testcase testcase, @PathParam("userID") String userID ) {
		//logger.info( "[REQ][CREATE][TESTCASE] Project = " + testcase.getProjectName()+ 
		//		", User = " + userID + ", Contents = " + testcase.getContents() );
		
		if ( projectService.AddProjectTestcase( testcase, userID ))
			return "SUCCESS create testcase";		
		return "Fail create testcase";
	}

	/**
	 * Request Testcase List
	 * @param projectID
	 * @return
	 */
	/*
	@GET
	@Path("/testcase/list/{projectID}")
	@Produces("application/xml")
	public List<Testcase> getMyTestcaseList( @PathParam("projectID") String projectID ) {
		//logger.info("[REQ][SELECT] Testcase List");
		return projectService.getMyTestcaseList(projectID);						
	}
	*/
	@GET
	@Path("/testcase/list/{projectID}")
	@Produces("text/plain")
	public String getMyTestcaseList( @PathParam("projectID") String projectID ) {
		//logger.info("[REQ][SELECT] Testcase List");
		return projectService.getMyTestcaseList(projectID);						
	}
	
	/**
	 * Delete Testcase
	 * @param projectID
	 * @param testcaseID
	 * @return
	 */
    @GET
    @Path("/{projectID}/testcase/{testcaseID}")
    public String deleteTestcase( @PathParam("projectID") String projectID, 
    		@PathParam("testcaseID") String testcaseID) {
    	    	        	
    	//logger.info("[REQ][DELETE] Testcase : ProjectID = " + projectID + ", testcaseID = " + testcaseID );
    	if ( projectService.deleteTestcase( projectID, testcaseID ))   
    		return "SUCCESS delete TESTCASE";
    	return "Fail delete TESTCASE";
    }
    
	/**
	 * Update Testcase
	 * @param testcase
	 * @return
	 */
    @POST //@PUT
    @Path("/{projectID}/testcase/{testcaseID}")
    public String modifyTestcase( Testcase testcase ) {
    	    	
    	//logger.info("[REQ][UPDATE] Testcase : ProjectID = " + testcase.getProjectName() + 
    	//		", Contents = " + testcase.getContents() );
    	
    	if ( projectService.modifyTestcase( testcase ))   
    		return "SUCCESS modify TESTCASE";
    	
    	return "Fail modify TESTCASE";
    }
}